/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.business.service.impl;

import me.zhengjie.business.domain.PricRgstIns;
import me.zhengjie.business.domain.PricRgstTrtappy;
import me.zhengjie.business.domain.vo.PricRgstVo;
import me.zhengjie.business.repository.PricRgstTrtappyRepository;
import me.zhengjie.business.service.mapstruct.PricRgstTrtappyMapper;
import me.zhengjie.modules.system.service.DeptService;
import me.zhengjie.modules.system.service.dto.DeptDto;
import me.zhengjie.utils.*;
import lombok.RequiredArgsConstructor;
import me.zhengjie.business.repository.PricRgstInsRepository;
import me.zhengjie.business.service.PricRgstInsService;
import me.zhengjie.business.service.dto.PricRgstInsDto;
import me.zhengjie.business.service.dto.PricRgstInsQueryCriteria;
import me.zhengjie.business.service.mapstruct.PricRgstInsMapper;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import cn.hutool.core.util.IdUtil;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://el-admin.vip
* @description 服务实现
* @author xiahongbo
* @date 2025-01-09
**/
@Service
@RequiredArgsConstructor
public class PricRgstInsServiceImpl implements PricRgstInsService {

    private final PricRgstInsRepository pricRgstInsRepository;
    private final PricRgstInsMapper pricRgstInsMapper;

    private final PricRgstTrtappyRepository pricRgstTrtappyRepository;
    private final PricRgstTrtappyMapper pricRgstTrtappyMapper;

    private final DeptService deptService;

    @Override
    public Map<String,Object> queryAll(PricRgstInsQueryCriteria criteria, Pageable pageable){
        Page<PricRgstIns> page = pricRgstInsRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(pricRgstInsMapper::toDto));
    }

    @Override
    public List<PricRgstInsDto> queryAll(PricRgstInsQueryCriteria criteria){
        return pricRgstInsMapper.toDto(pricRgstInsRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public PricRgstInsDto findById(String pricRgstInsId) {
        PricRgstIns pricRgstIns = pricRgstInsRepository.findById(pricRgstInsId).orElseGet(PricRgstIns::new);
        ValidationUtil.isNull(pricRgstIns.getPricRgstInsId(),"PricRgstIns","pricRgstInsId",pricRgstInsId);
        return pricRgstInsMapper.toDto(pricRgstIns);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public PricRgstInsDto create(PricRgstIns resources) {
        resources.setPricRgstInsId(IdUtil.simpleUUID()); 
        return pricRgstInsMapper.toDto(pricRgstInsRepository.save(resources));
    }

    @Override
    @Transactional
    public void create(PricRgstIns resources, PricRgstTrtappy appy) {
        pricRgstInsRepository.save(resources);
        pricRgstTrtappyRepository.save(appy);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(PricRgstIns resources) {
        PricRgstIns pricRgstIns = pricRgstInsRepository.findById(resources.getPricRgstInsId()).orElseGet(PricRgstIns::new);
        ValidationUtil.isNull( pricRgstIns.getPricRgstInsId(),"PricRgstIns","id",resources.getPricRgstInsId());
        pricRgstIns.copy(resources);
        pricRgstInsRepository.save(pricRgstIns);
    }

    @Override
    public void deleteAll(String[] ids) {
        for (String pricRgstInsId : ids) {
            pricRgstInsRepository.deleteById(pricRgstInsId);
        }
    }

    @Override
    public void download(List<PricRgstInsDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (PricRgstInsDto pricRgstIns : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}